<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习6</title>
</head>

<body>

</body>
<script>
    // 1.将一个字符串数组输出为|分割的形式，比如“刘备|张飞|关羽”。使用两种方式实现
    let name = ["刘备", "张飞", "关羽"];
    let result = name.join("|");
    console.log(result);

    let name1 = ["刘备", "张飞", "关羽"];
    let result1 = name1.toString().replace(/,/g, "|");
    console.log(result1);

    // 2.将一个字符串数组的元素的顺序进行反转。["a", "b", "c", "d"] -> [ "d","c","b","a"]。使用两种种方式实现。
    let arr = ["a", "b", "c", "d"];
    let reversed = arr.reverse();
    console.log(reversed);

    let arr1 = ["a", "b", "c", "d"];
    let reversed1 = [];
    for (let i = arr1.length - 1; i >= 0; i--) {
        reversed1.push(arr1[i]);
    }
    console.log(reversed1);

    // 3.工资的数组[1500, 1200, 2000, 2100, 1800],把工资超过2000的删除
    let salaries = [1500, 1200, 2000, 2100, 1800];
    let filtered = salaries.filter(function (salary) {
        return salary <= 2000;
    });

    console.log(filtered);

    // 4.["c", "a", "z", "a", "x", "a"]找到数组中每一个a出现的位置
    let arr2 = ["c", "a", "z", "a", "x", "a"];
    let position = [];
    let index = arr2.indexOf("a");

    while (index != -1) {
        position.push(index);
        index = arr2.indexOf("a", index + 1); // 从下一个位置继续查找
    }

    console.log(position);

    // 5.编写一个方法去掉一个数组的重复元素
    let arr3 = ["c", "a", "z", "a", "x", "a"];
    function removeDuplicate(arr) {
        return arr.filter(function (item, index) {
            return arr.indexOf(item) == index;
        });
    }
    console.log(removeDuplicate(arr3));
</script>

</html>